﻿'************************************************************************************'
'* Copyright (c) 2009, Patrick Driscoll All rights reserved.                        *'
'*                                                                                  *'
'* Redistribution and use in source and binary forms, with or without               *'
'* modification, are permitted provided that the following conditions are met:      *'
'*                                                                                  *'
'* Redistributions of source code must retain the above copyright notice, this      *'
'* list of conditions and the following disclaimer. Redistributions in binary       *'
'* form must reproduce the above copyright notice, this list of conditions and      *'
'* the following disclaimer in the documentation and/or other materials             *'
'* provided with the distribution. Neither the name of the DriscollArt Software nor *'
'* the names of its contributors may be used to endorse or promote products         *'
'* derived from this software without specific prior written permission.            *'
'*                                                                                  *'
'* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"      *'
'* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE        *'
'* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE       *'
'* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR      *'
'* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL           *'
'* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR       *'
'* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER       *'
'* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT               *'
'* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY        *'
'* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH      *'
'* DAMAGE.                                                                          *'
'************************************************************************************'

Imports System.Data.SqlClient

''' <summary>
''' SQL script execution dialog
''' </summary>
''' <remarks></remarks>
Public Class Dialog_CreateSQL

    Private Sub Button_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Cancel.Click
        Me.Close()
    End Sub

    Private Sub Dialog_CreateSQL_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If CheckBox_Remember.Checked Then
            My.Settings.LastConnectionString = TextBox_ConnectionString.Text
        Else
            My.Settings.LastConnectionString = ""
        End If
    End Sub

    Private Sub Dialog_CreateSQL_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If My.Settings.LastConnectionString <> "" Then
            TextBox_ConnectionString.Text = My.Settings.LastConnectionString
            CheckBox_Remember.Checked = True
        End If
    End Sub

    Private Sub Button_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Connect.Click

        Dim conn As New SqlConnection(TextBox_ConnectionString.Text)
        Dim cmd As New SqlCommand("", conn)
        'Dim strCommands As String() = TextBox_SQLOutput.Text.Replace(vbCrLf, " ").Replace("GO", ";").Split(";" & vbCrLf)
        Dim strCommands As String() = TextBox_SQLOutput.Text.Replace("GO", ";").Split(";" & vbCrLf)
        Dim previousConnectionState As ConnectionState
        previousConnectionState = conn.State
        Try
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
            cmd.CommandType = CommandType.Text

            For Each strCommand As String In strCommands
                Debug.Print(strCommand)
                cmd.CommandText = strCommand
                cmd.ExecuteNonQuery()
            Next

            MsgBox("SQL scripts executed successfully!", MsgBoxStyle.OkOnly, "SQL OK")

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.OkOnly, "SQL Error")
        Finally
            If previousConnectionState = ConnectionState.Closed Then
                conn.Close()
            End If
        End Try
    End Sub

    Private Sub Button_Clipboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Clipboard.Click
        My.Computer.Clipboard.SetText(TextBox_SQLOutput.Text)
        MsgBox("Script copied to clipboard!", MsgBoxStyle.OkOnly, "Clipboard")
    End Sub
End Class